#include "common.h"

static void print_page_header(const char* source_name, const char* date);

// Print a source line
void print_line(char* line, const char* source_name_to_print, const char* date_to_print, int line_number)
{
    char save_ch;
    char* save_chp = NULL;
    static int line_count = MAX_LINES_PER_PAGE;
    
    ++line_count;
    if(line_count > MAX_LINES_PER_PAGE)
    {
        print_page_header(source_name_to_print, date_to_print);
		line_count = 0;
    }

	printf("%4d: ", line_number);

    while(strlen(line) > MAX_PRINT_LINE_LENGTH) 
    {
        save_ch = line[MAX_PRINT_LINE_LENGTH];
        line[MAX_PRINT_LINE_LENGTH] = '\0';
        printf("%s\n", line);
        line[MAX_PRINT_LINE_LENGTH] = save_ch;
		line = line + MAX_PRINT_LINE_LENGTH;
    }
    printf("%s", line);
}

// Print the page header
static void print_page_header(const char* source_name, const char* date)
{
	char path[128];
    static int page_number = 0;
    ++page_number;
	getcwd(path, 128);
    printf("\n\nPage %4d %s/%s  %s\n\n", page_number, path, source_name, date);
}
